<?php

namespace app\speedDating\web;

use app\speedDating\interfaces\ServiceCode;
use think\response\Json;

/**
 * 统一处理响应数据
 */
class JsonResult
{
    private $status;
    private $msg;
    private $data;

    public static function success($data = [])
    {
        if (empty($data)) $data = [];
        $result = new JsonResult();
        $result->setStatus(ServiceCode::SUCCESS);
        $result->setData($data);
        return $result->__toJson();
    }

    public static function fail($state, $message)
    {
        $result = new JsonResult();
        $result->setStatus($state);
        $result->setMsg($message);
        return $result->__toJson();
    }

    /** @param $msgData ['label' => XXX, 'state' => 4XX] */
    public static function failMsgData($msgData = [])
    {
        $msgData['state'] = $msgData['state'] ?? 0;
        $msgData['label'] = $msgData['label'] ?? 'Unknown description';
        return self::fail($msgData['state'], $msgData['label']);
    }


    public static function failExceptionHandle($state, $message)
    {
        $result = new JsonResult();
        $result->setStatus($state);
        $result->setMsg($message);
        return $result->__toJson();
    }

    /**
     * @return mixed
     */
    private function getStatus()
    {
        return $this->status;
    }

    /**
     * @param mixed $state
     */
    private function setStatus($state): void
    {
        $this->status = $state;
    }

    /**
     * @return mixed
     */
    private function getMessage()
    {
        return $this->msg;
    }

    /**
     * @param mixed $message
     */
    private function setMsg($message): void
    {
        $this->msg = $message;
    }

    /**
     * @return mixed
     */
    private function getData()
    {
        return $this->data;
    }

    /**
     * @param mixed $data
     */
    private function setData($data): void
    {
        $this->data = $data;
    }

    public function __toJson(): Json
    {
        return json(get_object_vars($this));
    }

}